/*
 * FileObjectLocation.java
 *
 * Created on Oct 12, 2007, 8:36:09 PM
 *
 * Copyright (c) October 14, 2007 by Dean Del Ponte
 * http://www.sourceseed.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by  the Free Software Foundation; either version 3 of the License or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.TXT.  If not, write to
 * the Free Software Foundation Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307 USA
 */
package org.sourceseed.tools.backup.backThatThingUp;

import java.io.File;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.auth.StaticUserAuthenticator;
import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Dean Del Ponte
 */
public abstract class FileObjectLocation {

    static Logger logger = Logger.getLogger(FileObjectLocation.class);
    private String path;
    private FtpMode ftpMode;
    private String username;
    private String password;

    public FileObjectLocation() {
        this(null, null);
    }

    /**
     * 
     * @param path The path to the file object location
     * @param ftpMode Active or passive ftp
     */
    public FileObjectLocation(String path, FtpMode ftpMode) {
        this(path, ftpMode, null, null);
    }

    /**
     * 
     * @param path The path to the file object location
     * @param ftpMode Active or passive ftp
     * @param username Used for authentication if needed
     * @param password User for authentication if needed
     */
    public FileObjectLocation(String path, FtpMode ftpMode, String username, String password) {
        PropertyConfigurator.configure("config" + File.separator + "log4j.properties");
        this.path = path;
        this.ftpMode = ftpMode;
        this.username = username;
        this.password = password;
    }

    /**
     * 
     * @return The path to the file object location
     */
    public String getPath() {
        return path;
    }

    /**
     * 
     * @param path
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 
     * @return active or passive
     */
    public FtpMode getFtpMode() {
        return ftpMode;
    }

    /**
     * 
     * @param ftpMode
     */
    public void setFtpMode(FtpMode ftpMode) {
        this.ftpMode = ftpMode;
    }

    /**
     * 
     * @return FileObject reference to this file object location
     */
    public FileObject getFileObject() {
        if (ftpMode == null) {
            setFtpMode(FtpMode.PASSIVE);
        }
        FileObject destFileObject = null;
        try {
            StaticUserAuthenticator auth = new StaticUserAuthenticator(null, username, password);
            FileSystemManager fsManager = VFS.getManager();
            FileSystemOptions opts = new FileSystemOptions();
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, ftpMode.getMessage().equals(FtpMode.PASSIVE.getMessage()));
            destFileObject = fsManager.resolveFile(path, opts);
        } catch (FileSystemException ex) {
            logger.error("Error reading file " + path, ex);
        }
        return destFileObject;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return getPath();
    }
}
